<?php
namespace app\admin\controller\sum\supplier;

use addons\sum\library\pay\PayService;
use addons\sum\service\pay\PayOper;
use addons\sum\service\store\Agent as StroeService;
use app\admin\model\sum\Pay;
use think\Db;
use think\exception\HttpResponseException;
use addons\sum\exception\SumException;
use app\admin\model\sum\StockOrder as StockOrderModel;
use app\admin\model\sum\StockOrderExpress as StockOrderExpressModel;
use app\admin\model\sum\goods\Goods as GoodsModel;
use app\admin\model\sum\store\Goods as StoreGoodsModel;
use addons\sum\service\order\OrderDispatch as OrderDispatchService;

/**
 * 商户进货管理
 */
class StockOrder extends \app\admin\controller\sum\Common
{

    public function _initialize()
    {
        \app\admin\controller\sum\Common::_initialize();
        $this->model = new StockOrderModel;
    }


    /**
     * 商户进货列表
     */
    public function index()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }
        $records = $this->model->sheepFilter()->with(['goods','store','express'])->paginate($this->request->param('list_rows', 10));
        $this->success('获取成功', null, $records);
    }

    /**
     * 详情
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        if (!$detail) {
            $this->error(__('No Results were found'));
        }
        $this->success('获取成功', null, $detail);
    }


    public function dispatch()
    {
        if (!$this->request->isAjax()) {
            return $this->view->fetch();
        }

        $params = $this->request->only(['action', 'order_id', 'order_ids', 'order_item_ids', 'method', 'sender', 'express', 'order_express_id']);

        $action = $params['action'] ?? 'confirm';
        if (!in_array($action, ['confirm', 'cancel', 'change', 'multiple'])) {
            $this->error('发货参数错误');
        }
        switch ($action) {
            case 'confirm':
                // 发货操作
                $express=[];
                $admin = auth_admin();
                $method = $params['method'] ?? '';
                if (!in_array($method, ['input', 'api', 'upload'])) {
                    error_stop('请使用正确的发货方式');
                }
                $stock_order=$this->model->where('id',$params['order_id'])->find();
                if ($stock_order->status !== StockOrderModel::STATUS_PAID) {
                    error_stop("该订单不能发货");
                }
                switch ($method) {
                    case 'api':
                        break;
                    case 'input':
                        $express = $params['express'] ?? null;
                        if (empty($express['name']) || empty($express['code']) || empty($express['no']) || strpos($express['no'], '=') !== false) {
                            error_stop('请输入正确的快递信息');
                        }
                        break;
                    case 'upload':
                        break;
                }

                // 添加包裹信息
                $orderExpress = StockOrderExpressModel::create([
                    'store_id' => $stock_order->store_id,
                    'stock_order_id' => $stock_order->id,
                    'express_name' => $express['name'],
                    'express_code' => $express['code'],
                    'express_no' => $express['no'],
                    'method' => $method,
                    'driver' => $express['driver'] ?? null,
                    'ext' => $express['ext'] ?? null
                ]);
                $stock_order->status = 'noget';
                $stock_order->stock_order_express_id = $orderExpress['id'];
                if($stock_order->ext){
                    $stock_order->ext = array_merge($stock_order->ext, ['send_time' => time()]);    // item 发货时间
                }else{
                    $stock_order->ext = ['send_time' => time()];    // item 发货时间
                }
                $stock_order->save();
                $this->success('发货成功', null, $express);
                break;
        }

        $this->error('操作失败');
    }



}
